package rpg.data;

import java.io.File;
import java.util.List;

import org.jdom.Element;

import pedrosm.data.ScriptData;


import square.data.DataGroup;
import square.data.SkillData;
import square.script.SkillScript;

/**
 * 
 * @author Pedro Silva Moreira
 */
public class SkillXML implements LoaderXML {
	private static SkillXML singleton = new SkillXML();
	
	// Creation
	private SkillXML() {
	}
	
	// Instance
	public static SkillXML instance() {
		return singleton;
	}
	
	// Load
	@SuppressWarnings("unchecked")
	public void load(String group, String name, Element origin) {
		Element elem = XML.loadXML(XML.DATA_FOLDER + "skills" + File.separator
				+ group + File.separator + name + ".xml");

		// Name
		SkillData data = new SkillData();
		data.setName(elem.getAttributeValue("name"));
		
		// Group
		data.setGroup(group);

		// Thumb
		String thumb = elem.getAttributeValue("thumb");
		data.setThumb(DataGroup.findSprite(thumb));

		// Script
		String script = elem.getAttributeValue("script");
		if (script != null)
			data.setScript((ScriptData<SkillScript>) DataGroup.findScript(script));

		// Attributes
		List<Element> attributes = elem.getChildren("attr");
		for (Element it : attributes) {
			String attributeName = it.getAttributeValue("name");
			String attributeValue = it.getAttributeValue("value");

			if (attributeName.equals("delay")) {
				data.setDelay(Integer.valueOf(attributeValue));
			} else if (attributeName.equals("exhaust")) {
				data.setExhaust(Integer.valueOf(attributeValue));
			} else if (attributeName.equals("level")) {
				data.setLevel(Integer.valueOf(attributeValue));
			} else if (attributeName.equals("target")) {
				data.setTarget(Boolean.valueOf(attributeValue));
			}
		}

		// Type
		List<Element> types = elem.getChildren("type");
		for (Element it : types) {
			data.getTypes().add(it.getAttributeValue("name"));
		}

		// Add to Data
		DataGroup.getSkills().add(data);
	}
}
